package com.zx.dao;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zx.domain.Student;
import com.zx.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class StudentDao {

    //  将表中的所有学生信息取出
    public static List<Student> getStudents() throws SQLException {

        QueryRunner qr = new QueryRunner();
        String sql = "select * from student";
        Connection con = DruidUtils.getConnection();
        List<Student> students = qr.query(con, sql, new BeanListHandler<Student>(Student.class));
        con.close();
        return students;
    }

    //  添加学生信息
    public static boolean addStudent(Student student) throws SQLException {

        List<Student> students = getStudents();
        if(students.size()!=0){
            for(Student s : students){
                if(s.getId()==student.getId()){
                    return false;
                }
            }
            QueryRunner qr = new QueryRunner();
            String sql = "insert into student values(?,?,?,?)";
            Connection con = DruidUtils.getConnection();
            Object[] param = {student.getId(),student.getSname(),student.getGender(),student.getBirthday()};
            qr.update(con, sql, param);
            con.close();
            return true;
        }else{
            QueryRunner qr = new QueryRunner();
            String sql = "insert into student values(?,?,?,?)";
            Connection con = DruidUtils.getConnection();
            Object[] param = {student.getId(),student.getSname(),student.getGender(),student.getBirthday()};
            qr.update(con, sql, param);
            con.close();
            return true;
        }
    }

    //  删除学生信息
    public static boolean deleteStudent(int id) throws SQLException {

        List<Student> students = getStudents();
        for(Student s : students){
            if(s.getId()==id){
                QueryRunner qr = new QueryRunner();
                String sql = "delete from student where id=?";
                Connection con = DruidUtils.getConnection();
                qr.update(con, sql, id);
                con.close();
                return true;
            }
        }
        return false;
    }

    //  查找学生信息
    public static String queryStudent(int id) throws SQLException, JsonProcessingException {

        List<Student> students = getStudents();
        for(Student s : students){
            if(s.getId()==id){
                Student stu = new Student(s.getId(), s.getSname(),s.getGender(),s.getBirthday());
                ObjectMapper om = new ObjectMapper();
                String ss = om.writeValueAsString(stu);
                return ss;
            }
        }
        return null;
    }

    //  显示所有学生信息
    public static String printStudent() throws SQLException, JsonProcessingException {

        List<Student> students = getStudents();
        ObjectMapper om = new ObjectMapper();
        String s = om.writeValueAsString(students);
        return s;
    }

}
